﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class GSAB50Data : BaseFunction
    {


        public GSAB50Data()
            : base()
        {

        }

        public GSAB50Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public void GetGABEW(DataRow dwABPOS)
        {

            string TableName = "GABEW";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = " SELECT MIN(GABBUCHDAT) AS ACTENDDATE FROM GABEW WHERE GABBS = '13' "
                        + " AND GABAUFKRS =:GABAUFKRS  AND GABAUFLFD =:GABAUFLFD  AND GABAUFPOS = : GABAUFPOS "
                    ;
                orlReadCommand.Parameters.Add("GABAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                orlReadCommand.Parameters.Add("GABAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                orlReadCommand.Parameters.Add("GABAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;
                    dsResult.AcceptChanges();
                    foreach (DataRow rowGABEW in dsResult.Tables[TableName].Rows)
                    {
                        dwABPOS["ACTENDDATE"] = rowGABEW["ACTENDDATE"];
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        public DataSet BuildData()
        {
            DataSet setReturn = new DataSet();
            DataSet dsResult = new DataSet();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT A.ABPAUFKRS,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR, "
                    + " TRIM(B.LSNAME1) AS FACTORY,TRIM(C.GSCBEZ) AS COLOR, "
                    + " A.ABPFARBE,A.ABPSOLLMENGE5/100 AS QUANTITY,A.ABPSTARTDAT,A.ABPENDDAT,'' AS ACTENDDATE "
                    + " FROM ABPOS A,LIEFDAT B,GSCOLOR C WHERE A.ABPMANDANT = B.LSMANDANT(+) AND A.ABPLIEFNR = B.LSLIEFNR(+) "
                    + " AND A.ABPMANDANT = C.GSCMANDANT(+) AND A.ABPART = C.GSCART(+) AND A.ABPVAR = C.GSCVAR(+)  "
                    + " AND A.ABPFARBE = C.GSCFARBE(+) AND A.ABPAUFART = 'GP' "
                    //  + " AND A.ABPAUFKRS = 'DB' AND A.ABPAUFLFD = '001213' "
                    ;

                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABPOS";


                    byte[] b;
                    string value;
                    string column = "";
                    foreach (DataRow dwTemp in dsResult.Tables["ABPOS"].Rows)
                    {
                        column = "FACTORY";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "COLOR";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "ABPFARBE";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        GetGABEW(dwTemp);
                    }
                }
                setReturn.Merge(dsResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}